package com.yazi.mxz.job.quartz;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.myframework.smhj.common.tool.DateUtil;
import com.yazi.mxz.common.entity.beans.GoodsPromote;
import com.yazi.mxz.common.entity.enums.GoodsPromoteType;
import com.yazi.mxz.common.service.GoodsPromoteService;

/**
 * 开始商品促销的线程
 * @author mo_yq5
 * @date 2014-11-27
 */
public class GoodsPromoteStartJob extends MyJob {
	private static Logger log = LoggerFactory.getLogger(GoodsPromoteStartJob.class);
	private static Long nextTime = null;
	private static GoodsPromoteType type = GoodsPromoteType.ONLINE_DISCOUNT;
	
	@Resource
	GoodsPromoteService goodsPromoteService;
	
	@Override
	public void run() {
		goodsPromoteService.startByTime(type);
		GoodsPromote gp = goodsPromoteService.oneNextStart();
		if (null == gp) {
			nextTime = null;
			return;
		}
		type = GoodsPromoteType.values()[gp.getId().getType()];
		nextTime = gp.getStartTime();
		Long curTimeStamp = DateUtil.getTimeStamp();
		if (nextTime > curTimeStamp) {
			try {
				Thread.sleep(nextTime - curTimeStamp);
			} catch (InterruptedException e) {
				log.info(e.getMessage());
			}
		} 
		run();
	}

	@Override
	public void checkDoit(long timeStamp) {
		if (null == nextTime || nextTime > timeStamp) {
			doit();
		}
	}

	public static void setType(GoodsPromoteType type) {
		GoodsPromoteStartJob.type = type;
	}

}
